//
//  WKTransactionViewController.m
//  ourToken
//
//  Created by wakeup on 2018/5/29.
//  Copyright © 2018年 ourtoken. All rights reserved.
//

#import "WKTransactionViewController.h"
#import "WKBaseHeader.h"

@interface WKTransactionViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *transactionArray;//数据源

@end

@implementation WKTransactionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationView];
    [self layoutUI];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"WKTransactionTableViewCell" bundle:nil] forCellReuseIdentifier:@"TransactionCell"];
}

- (void)setNavigationView {
    [super setNavigationView];
    self.navigationView.topTitleLabel.text = @"交易";
    self.navigationView.topLeftButton.hidden = YES;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [self getTransactionListNetworking];
}

- (void)layoutUI {
    [self.view addSubview:self.tableView];
}

//获取交易列表
- (void)getTransactionListNetworking {
    [httpManager getTransactionListWithPageIndex:1 pageSize:10 success:^(NSDictionary *result) {
        
        [self.transactionArray removeAllObjects];
        NSDictionary *data = [result objectForKey:@"data"];
        for (NSDictionary *dict in data) {
            WKTransactionModel *list = [WKTransactionModel mj_objectWithKeyValues:dict];
            [self.transactionArray addObject:list];
        }
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.transactionArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WKTransactionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TransactionCell" forIndexPath:indexPath];
    WKTransactionModel *transactionModel = self.transactionArray[indexPath.row];
    cell.transactionModel = transactionModel;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    WKTransactionHistoryViewController *history = [[WKTransactionHistoryViewController alloc] init];
    [self.navigationController pushViewController:history animated:YES];
}

#pragma mark - 懒加载
- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, AppManager.navigationHeight, AppManager.width, AppManager.height-AppManager.navigationHeight-AppManager.tabBarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc] init];
    }
    return _tableView;
}

- (NSMutableArray *)transactionArray {
    if (_transactionArray == nil) {
        _transactionArray = [[NSMutableArray alloc]init];
    }
    return _transactionArray;
}

@end
